<template>
	<view class="app-container">

		<view class="order-header">
			<view class="header-item" :class="currentSelectIndex === index  ? 'header-item-active' : ''"
				@tap="tabChangeHandle(index)" v-for="(item,index) in tabList" :key="index">{{ item.name }}</view>
		</view>

		<view class="order-list-container">
			<view class="order-item-box" v-for="(item,index) in orderList" :key="index">
				<view class="order-item-content">
					<view class="header-box">
						<view class="header-left">{{ '共' + item.orderLines.length + '件商品' }}</view>
						<view class="header-right">{{ item.statusText }}</view>
					</view>
					<view class="order-line-box" v-for="(line,index) in item.orderLines" :key="index">
						<view class="order-pics">
							<image :src="line.skuPic" class="sku-pic" mode=""></image>
						</view>
						<view class="order-line-content">
							<view class="sku-name">
								{{ line.skuName }}
							</view>
							<view class="order-line-more">
								<text>
									<text class="line-price-prefix">¥</text>
									<text class="line-total-amount">{{ item.totalAmount }}</text></text>
								<text class="total">x{{ line.skuQuantity }}</text>
							</view>
						</view>
					</view>
					<view class="more">
						<text>
							<text style="font-size: 26rpx;font-weight: 500">实付款</text>
							<text class="price-prefix">¥</text>
							<text class="total-amount">{{ item.totalAmount }}</text></text>
						<text class="total">共{{ item.orderLines.length }}件</text>
					</view>
					<!-- <view class="order-btn-box">
						<view class="order-btn-item" style="background-color: #DE3B2E;color: #FFF;border: none;">再次购买</view>
					</view> -->
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import $http from '@/common/api/request.js'
	export default {
		data() {
			return {
				queryParams: {
					pageNo: 1,
					pageSize: 10
				},
				currentSelectIndex: 0,
				tabList: [{
						name: '全部',
						code: -1
					},
					{
						name: '待付款',
						code: 0
					},
					{
						name: '待发货',
						code: 1
					},
					{
						name: '待收货',
						code: 2
					},
					{
						name: '已完成',
						code: 3
					}
				],
				total: -1,
				orderList: []
			}
		},
		onReachBottom() {
			this.queryParams.pageNo += 1
			this.loadData()
		},
		onBackPress(e) {
			uni.switchTab({
				url: '/pages/me/me'
			});
			return true;
		},
		onShow() {
			this.loadData()
		},
		methods: {
			tabChangeHandle(index) {
				this.orderList = []
				this.queryParams = {
					pageNo: 1,
					pageSize: 10
				}
				this.currentSelectIndex = index
				if (this.tabList[index].code === -1) {
					delete this.queryParams.status
				} else {
					this.queryParams.status = this.tabList[index].code
				}
				this.loadData()
			},
			loadData() {
				if (this.orderList.length === this.total) {
					return
				}
				$http.request({
					url: '/order/order/memberOrder',
					data: this.queryParams,
					loading: true
				}).then(res => {
					if (res.success) {
						res.data.records.forEach(item => {
							if (item.orderLines.length > 3) {
								item.orderLines = item.orderLines.slice(0, 3)
							}
						})
						this.orderList.push(...res.data.records)
						this.total = res.data.total
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.app-container {
		background-color: #F0F1F5;
	}

	.order-header {
		top: 78rpx;
		display: flex;
		position: fixed;
		width: 100%;
		z-index: 99;
		height: 80rpx;
		font-size: 24rpx;
		line-height: 80rpx;
		background-color: #F7F7F7;
		justify-content: center;
		align-items: center;
	}

	.order-list-container {
		margin-top: 80rpx;
	}

	.header-item {
		text-align: center;
		flex: 1;
	}

	.header-item-active {
		border-bottom: 5rpx solid #DE3B2E;
	}

	.order-item-box {
		background-color: #FFF;
		width: 95%;
		margin: 0 auto;
		/* height: 280rpx; */
		margin-top: 20rpx;
		border-radius: 10rpx;
	}

	.order-item-content {
		padding: 20rpx;
	}

	.header-box {
		display: flex;
		justify-content: space-between;
		height: 50rpx;
	}

	.header-left {
		font-weight: 500;
		font-size: 28rpx;
		width: 50%;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.header-right {
		font-size: 24rpx;
		color: #878787;
	}

	.order-line-box {
		display: flex;
		justify-content: space-between;

		.order-line-content {
			padding: 0 0 0 10rpx;
			display: flex;

			.sku-name {
				font-size: 26rpx;
				height: 75rpx;
				max-width: 80%;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				/* 最多显示两行 */
				-webkit-box-orient: vertical;
				overflow: hidden;
				/* 溢出隐藏 */
				text-overflow: ellipsis;
				/* 使用省略号表示溢出部分 */
			}
			
			.order-line-more {
				display: flex;
				flex-direction: column;
				.line-price-prefix {
					font-size: 24rpx;
				}
				
				.line-total-amount {
					font-size: 26rpx;
				}
				
				.total {
					align-items: flex-end;
					align-self: flex-end;
				}
			}
		}
	}

	.order-pics {
		display: flex;
	}

	.sku-pic {
		width: 180rpx;
		height: 180rpx;
	}

	.order-btn-box {
		display: flex;
		margin-top: 15rpx;
		justify-content: flex-end;
	}

	.order-btn-item {
		padding: 10rpx 20rpx;
		margin-left: 10rpx;
		border: 1px #CDCDCD solid;
		border-radius: 10rpx;
		font-size: 26rpx;
	}

	.more {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-end;
	}

	.total {
		color: #878787;
		font-size: 24rpx;
	}

	.price-prefix {
		font-weight: 500;
		font-size: 24rpx;
		margin-left: 5rpx;
	}

	.total-amount {
		font-weight: 500;
	}
</style>